#include "stdafx.h"
#include "title.h"


Title::Title(QGraphicsItem *_source, QString _text, QPointF _pos){
	setTitle(_text);
	source = _source;
	position = _pos;
	setFlag(ItemIsMovable);
	setFlag(ItemIsSelectable);
	setFlag(ItemSendsGeometryChanges);
	setCacheMode(DeviceCoordinateCache);
	adjust();
}

QRectF Title::boundingRect() const{
	return size.normalized().adjusted(-3, 0, 3, 0);
}

void Title::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *){
	int a;
	if (isEnabled()) a = 255;
	else a = 100;
	painter->setPen(QPen(QColor(0, 0, 0, a), 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
	QFont font;
	font.setPointSize(9);
	painter->setFont(font);
	painter->setPen(Qt::black);
	painter->drawText(size, Qt::AlignVCenter, text);
}

void Title::mousePressEvent(QGraphicsSceneMouseEvent *event){
	update();
	QGraphicsItem::mousePressEvent(event);
}

void Title::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
	
	position.setX(source->pos().x() - this->pos().x());
	position.setY(source->pos().y() - this->pos().y());
	
	update();
	QGraphicsItem::mouseReleaseEvent(event);
}

void Title::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event){
	source->setSelected(true);
}

void Title::adjust(){
	setX(source->pos().x() - position.x());
	setY(source->pos().y() - position.y());
}
